package com.xbongbong.parent.abstracts;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.LinkConditionPoJo;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.condition.ConditionFieldHandler;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.pro.condition.pojo.ConditionDTO;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/03/07 16:25
 */
public abstract class AbstractConditionFieldHandler implements ConditionFieldHandler {

    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo) throws XbbException {
        return data.get(fieldAttrEntity.getAttr());
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterConditionsPoJo) throws XbbException {
        Object object = businessRuleInfoPojo.getObject();
        if (Objects.isNull(object)) {
            object = data.get(fieldAttrEntity.getAttr());
        }
        return object;
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        return filterCondition.getValue();
    }

    @Override
    public Object handlerRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, LinkConditionPoJo linkConditionPoJo, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        // TEXT, TEXTAREA, NUM
        if (value instanceof Map) {
            Map<String, Object> valueMap = (HashMap) value;
            value = valueMap.getOrDefault("text", "");
        }
        return value;
    }

    @Override
    public Object handlerLinkDataFixedValue(String corpid, FieldAttrEntity targetField, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        return value;
    }

    @Override
    public Object handlerBusinessRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, LinkConditionPoJo linkConditionPoJo, Object value, Long linkFormId, Integer linkBusinessType, Integer linkSaasMark) throws XbbException {
        // TEXT, TEXTAREA, NUM
        return value;
    }

    @Override
    public Object handlerBusinessDataFixedValue(String corpid, FieldAttrEntity targetField, ConditionDTO conditionDTO, Object value) throws XbbException {
        return value;
    }

}
